package com.zhongjian.technologycal.mall.activity.personal.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zhongjian.technologycal.mall.R;
import com.zhongjian.technologycal.mall.base.BaseActivity;
import com.zhongjian.technologycal.mall.bean.BaseBean;
import com.zhongjian.technologycal.mall.net.HttpRetrofitClient;
import com.zhongjian.technologycal.mall.utils.LoadingDialog;
import com.zhongjian.technologycal.mall.utils.NetWorkUtils;
import com.zhongjian.technologycal.mall.utils.StatusBarUtil;
import com.zhongjian.technologycal.mall.utils.ToastUitl;
import com.zhongjian.technologycal.mall.utils.UserUtils;
import com.zhongjian.technologycal.mall.view.PasswordInputView;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import rx.Subscriber;

/**
 * Created by wei on 2017/12/25.
 * 转让现金币的页面实现
 */

public class AssignmentCashActivity extends BaseActivity {
    @BindView(R.id.tv_main_title)
    TextView tv_main_title;
    @BindView(R.id.assignment_et_userid)
    EditText assignment_et_userid;
    @BindView(R.id.assignment_et_money)
    EditText assignment_et_money;
    @BindView(R.id.assignment_tv_money)
    TextView assignment_tv_money;
    private String strMoney;
    private Double money;
    private int sysID;
    private Dialog hintDialog = null;

    @Override
    protected void beforeInit() {
        super.beforeInit();
//        StatusBarUtil.setColorNoTranslucent(this, getResources().getColor(R.color.border_color));
//        StatusBarUtil.setMiuiStatusBarDarkMode(this, true);

    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tv_main_title.setText("转让现金币");
        strMoney = getIntent().getStringExtra("strMoney");
        assignment_tv_money.setText("" + strMoney);
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_assignment_cash;
    }

    @OnClick(R.id.iv_back_page)
    public void onClickBack() {
        finish();
    }

    @Override
    public void initPresenter() {

    }

    @OnClick(R.id.tv_assignment_commit)
    public void onClickAssigment() {
        if (!NetWorkUtils.isNetConnected(getBaseContext())) {
            ToastUitl.showShort(R.string.no_net);
            return;
        } else if (TextUtils.isEmpty(assignment_et_userid.getText().toString().trim())) {
            ToastUitl.showShort("输入众健号或者电话不能为空～");
            return;

        } else if (TextUtils.isEmpty(assignment_et_money.getText().toString().trim())) {
            ToastUitl.showShort("输入金额不能为空～");
            return;
        } else if (Double.valueOf(assignment_et_money.getText().toString().trim()) > Double.valueOf(strMoney)) {
            ToastUitl.showShort("输入金额超出可转金额～");
            return;

        } else if (Integer.valueOf(assignment_et_money.getText().toString().trim()) <= 0) {
            ToastUitl.showShort("输入金额必须大于0～");
            return;
        }

        hintDialog = new Dialog(AssignmentCashActivity.this, R.style.dialog_style1);
        LinearLayout layout;
        LayoutInflater inflater = (LayoutInflater) AssignmentCashActivity.this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        layout = (LinearLayout) inflater.inflate(R.layout.dialog_hint,
                null);
        final PasswordInputView tv_hint = (PasswordInputView) layout
                .findViewById(R.id.again_paypswd_pet);
        layout.findViewById(R.id.dialog_btn_cancel).setOnClickListener(
                new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        hintDialog.dismiss();
                    }
                });
        layout.findViewById(R.id.dialog_btn_ensure).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (TextUtils.isEmpty(tv_hint.getText().toString().trim())) {
                            ToastUitl.showShort("支付密码不能为空");
                            return;
                        } else if (tv_hint.getText().toString().trim().length() < 6) {
                            ToastUitl.showShort("请输入6位的支付密码");
                            return;
                        }
                        initRequestPayPassword(tv_hint.getText().toString().trim());
                        hintDialog.dismiss();
                    }
                });
        hintDialog.setContentView(layout);
        hintDialog.show();

    }


    private void initRequestPayPassword(String password) {
        HttpRetrofitClient.getInstance(getBaseContext())
                .postUrlRequestPayPassword(UserUtils.getUserToken(),
                        password, new Subscriber<ResponseBody>() {
                            @Override
                            public void onStart() {
                                super.onStart();
                            }

                            @Override
                            public void onCompleted() {

                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onNext(ResponseBody responseBody) {
                                try {
                                    Gson gson = new Gson();
                                    BaseBean baseBean = gson.fromJson(responseBody.string(), BaseBean.class);
                                    if (baseBean.getError_code() == 0) {
                                        initViewRequest();
                                    } else if (baseBean.getError_code() == 3) {
                                        Intent intent = new Intent();
                                        intent.setAction("com.broadcast.info_out_line");
                                        sendBroadcast(intent);
                                    } else {
                                        ToastUitl.showShort(baseBean.getError_message());
                                    }

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }


                            }
                        });

    }


    private void initViewRequest() {
        HttpRetrofitClient.getInstance(getBaseContext())
                .postUrlRequestOfMoney(UserUtils.getUserToken(),
                        Double.valueOf(assignment_et_money.getText().toString().trim()),
                        Integer.valueOf(assignment_et_userid.getText().toString().trim()),
                        new Subscriber<ResponseBody>() {
                            @Override
                            public void onStart() {
                                super.onStart();
                                LoadingDialog.showDialogForLoading(AssignmentCashActivity.this);
                            }

                            @Override
                            public void onCompleted() {

                            }

                            @Override
                            public void onError(Throwable e) {
                                LoadingDialog.cancelDialogForLoading();
                            }

                            @Override
                            public void onNext(ResponseBody responseBody) {
                                LoadingDialog.cancelDialogForLoading();
                                try {
                                    Gson gson = new Gson();
                                    BaseBean baseBean = gson.fromJson(responseBody.string(), BaseBean.class);
                                    if (baseBean.getError_code() == 0) {
                                        ToastUitl.showShort("转出成功～");
                                        finish();
                                    } else if (baseBean.getError_code() == 3) {
                                        Intent intent = new Intent();
                                        intent.setAction("com.broadcast.info_out_line");
                                        sendBroadcast(intent);
                                    } else {
                                        ToastUitl.showShort(baseBean.getError_message());
                                    }

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }

                            }
                        });
    }


}
